class StationController < ApplicationController
  before_filter :admin?

  def manage
    session[:station_id] = params[:id]
    @station = Station.find(params[:id])
    @station_round = StationRound.new
    @station_rounds = StationRound.paginate(:all, :page=>params[:page], :per_page=>10, :conditions=>["station_id=?", params[:id]], :order=>"id desc")
    render :action=>"manage", :layout=>"main"
  end

  def add_round
    s = Station.find(session[:station_id])

    sr = StationRound.new(params[:station_round])
    sr.station_id = session[:station_id]
    sr.user_id = session[:user_id]
    sr.save

    for i in 0..(sr.end_date-sr.start_date) do
      book_date = nil
      date_type = sr.date_type.to_i
      loop_date = sr.start_date+i
      if date_type<=6
        if loop_date.wday==date_type
          book_date = loop_date
        end
      elsif date_type==7
        if loop_date.wday>=1 && loop_date.wday<=5
          book_date = loop_date
        end
      elsif date_type==8
        if loop_date.wday==0 || loop_date.wday==6
          book_date = loop_date
        end
      else
        book_date = loop_date
      end
      if book_date
        course = Course.find(s.auto_course_id)
        if course
          for round in 1..sr.total_slot do
            ss = StationSlot.find(:first, :conditions=>["book_date=? AND start_time=? AND end_time=? AND course_id=? AND station_id=? AND round_no=?", book_date, sr.start_time, sr.end_time, course.id, s.id, round])
            if !ss
              ss = StationSlot.new
              ss.book_date = book_date
              ss.start_time = sr.start_time
              ss.end_time = sr.end_time
              ss.course_id = course.id
              ss.round_no = round
              ss.station_id = s.id
              ss.station_round_id = sr.id
              ss.serial = "#{book_date}-#{s.id}-#{sr.id}-#{round}"
              ss.gencode = random_string(20)
              ss.save
            end
          end
        end
      end
    end
    redirect_to :action=>"manage", :id=>session[:station_id]
  end

  def random_string(len)
    #generate a random password consisting of strings and digits
    #chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    #chars = ("A".."Z").to_a + ("0".."9").to_a
    chars = ("a".."z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    newpass = newpass.gsub('I','1')
    newpass = newpass.gsub('l','1')
    newpass = newpass.gsub('o','1')
    newpass = newpass.gsub('O','1')
    newpass = newpass.gsub('0','1')
    return newpass
  end

  def station_calendar
    @station = Station.find(session[:station_id])
    @month = nil
    @year = nil
    if params[:month]
      @month = "#{params[:month]}".to_i
    end
    if params[:year]
      @year = "#{params[:year]}".to_i
    end
    if !@month
      @month = Date.today.month
    end
    if !@year
      @year = Date.today.year
    end
    session[:c_month] = @month
    session[:c_year] = @year
    render :action=>"station_calendar", :layout=>"main"
  end

  def previous_year
    redirect_to :action=>"station_calendar", :month=>session[:c_month], :year=>session[:c_year]-1
  end

  def previous_month
    if params[:c_month]==1
      redirect_to :action=>"station_calendar", :month=>12, :year=>session[:c_year]-1
    else
      redirect_to :action=>"station_calendar", :month=>session[:c_month]-1, :year=>session[:c_year]
    end
  end

  def next_month
    if params[:c_month]==12
      redirect_to :action=>"station_calendar", :month=>1, :year=>session[:c_year]+1
    else
      redirect_to :action=>"station_calendar", :month=>session[:c_month]+1, :year=>session[:c_year]
    end
  end

  def next_year
    redirect_to :action=>"station_calendar", :month=>session[:c_month], :year=>session[:c_year]+1
  end

  def day_slot
    @station = Station.find(session[:station_id])
    @book_date = Date.parse("#{params[:id]}")
    @slots = StationSlot.find(:all, :conditions=>["book_date=? AND station_id=?", @book_date, session[:station_id]], :order=>"start_time, end_time, serial")
    render :action=>"day_slot", :layout=>"main"
  end

  def delete_slot
    StationSlot.find(params[:id]).destroy
    redirect_to :action=>"day_slot", :id=>params[:date]
  end

  def delete_round
    sr = StationRound.find(params[:id])
    slots = StationSlot.find(:all, :conditions=>["station_round_id=? AND s_status=?", sr.id, "Free"])
    slots.each do |slot|
      slot.destroy
    end
    sr.destroy
    redirect_to :action=>"manage", :id=>session[:station_id]
  end
end
